<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">Login</div>
      <el-form :model="data.form" ref="formRef" :rules="data.rules">
        <el-form-item prop="username">
          <el-input
              v-model="data.form.username"
              size="large"
              placeholder="Email"
              :prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              v-model="data.form.password"
              size="large"
              placeholder="Password"
              show-password
              :prefix-icon="Lock"
          />
        </el-form-item>

        <el-form-item>
          <el-button size="large" type="primary" class="login-btn" @click="login">Log in</el-button>
        </el-form-item>
        <div class="register-link">
          Don’t have an account? <a>Register</a>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { User, Lock } from "@element-plus/icons-vue";
import request from "@/utils/request";
import { ElMessage } from "element-plus";

const data = reactive({
  form: {
    username: "",
    password: "",
  },

  rules: {
    username: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
    password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  },
});

const formRef = ref();

const login = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      request.post("/admin/login", data.form).then((res) => {
        if (res.code === "200") {
          ElMessage.success("登录成功");
          localStorage.setItem("user", JSON.stringify(res.data));
          location.href = "/home";
        } else {
          ElMessage.error(res.msg);
        }
      });
    }
  });
};
</script>

<style scoped>
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-image: url('@/assets/imgs/loginbackword.png'); /* 替换为实际背景路径 */
  background-size: cover;
  background-position: center;
}

.login-box {
  background-color: rgba(255, 255, 255, 0.15);
  padding: 30px 40px;
  border-radius: 15px;
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  width: 360px;
}

.login-header {
  font-size: 24px;
  font-weight: bold;
  color: white;
  text-align: center;
  margin-bottom: 20px;
}

.login-btn {
  width: 100%;
  background: linear-gradient(to right, #6a11cb, #2575fc);
  color: white;
  border: none;
}

.register-link {
  text-align: center;
  color: white;
  font-size: 14px;
  margin-top: 10px;
}

.register-link a {
  color: #00aaff;
  text-decoration: none;
}

.register-link a:hover {
  text-decoration: underline;
}
</style>
